package com.example.administrator.acmapp.Activity;

import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.administrator.acmapp.Fragment.ClassroomFragment;
import com.example.administrator.acmapp.Fragment.CommunityFragment;
import com.example.administrator.acmapp.Fragment.QuestionFragment;
import com.example.administrator.acmapp.Fragment.DynamicFragment;
import com.example.administrator.acmapp.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    public SlidingMenu menu;
    private ListView menuListView;
    String[] menuNameArray;
    List<Integer> menuIconList;
    List<Map<String, Object>> menuList;
    int top;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initSlidingMenu();
        top = getTitleTop();
        CommunityFragment mCommunityFragment = new CommunityFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.tb, mCommunityFragment).commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 判断当前SDK版本号，如果是4.4以上，就是支持
        // 沉浸式状态栏的
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            ((RelativeLayout) findViewById(R.id.slidingmenu_head_top)).setPadding(0, top, 0, 0);
        }
    }
    private int getTitleTop() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = getResources().getDimensionPixelSize(x);// 状态栏高度
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        Log.e("状态栏高度:", sbar + ":" + x);
        return sbar;
    }
    public void initSlidingMenu() {
        //创建弹出菜单并设置菜单样式
        menu = new SlidingMenu(MainActivity.this);
        //为菜单设置各种样式
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);//全屏
        menu.setBehindWidth(700);
        menu.setBehindScrollScale(1);
        //将当前的弹出菜单绑定到指室的Activity中，并作为内容值
        menu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
        //为弹出菜单指定要显示的具体布局文件
        menu.setMenu(R.layout.main_slidingmenu_layout);
        menu.setOffsetFadeDegree(0.4f);
        initSetListViewMenu();
    }

    private void initData() {
        menuNameArray = getResources().getStringArray(R.array.menuarray);

        menuList = new ArrayList<>();
        for (int i = 0; i < menuNameArray.length; i++) {
            //根据用户权限初始化数据

            Map map = new HashMap();
            map.put("name", menuNameArray[i]);
            menuList.add(map);
        }
        menuIconList = new ArrayList();
        menuIconList.add(R.drawable.dynamic);
        menuIconList.add(R.drawable.question);
        menuIconList.add(R.drawable.collect);
        menuIconList.add(R.drawable.record);
        menuIconList.add(R.drawable.signdesk);
        menuIconList.add(R.drawable.leave);
        menuIconList.add(R.drawable.tool);
    }

    private void initSetListViewMenu() {
        menuListView = (ListView) findViewById(R.id.menu_listview);
        MyBaseMenuAdapter menuAdapter = new MyBaseMenuAdapter();
        menuListView.setAdapter(menuAdapter);
        menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = (String) menuList.get(i).get("name");
                switch (name) {

                    case "我的动态":
                        Toast.makeText(MainActivity.this, "我的动态", Toast.LENGTH_SHORT).show();
                        menu.toggle();
                        break;
                    case "我的问答":
                        break;
                    case "我的收藏":
                        break;
                }
            }
        });

    }

    private void initView() {
        final BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setInActiveColor("#D5CECE").setActiveColor("#7fd8bf").setBarBackgroundColor("#ffffff");
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.shequ_foused, "社区")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.shequ_unfoused)))
                .addItem(new BottomNavigationItem(R.drawable.classroom_focused, "课堂")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.classroom_unfocused)))
                .addItem(new BottomNavigationItem(R.drawable.question_focused, "问答")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.question_nofocused)))
                .addItem(new BottomNavigationItem(R.drawable.talk_foucused, "动态")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.talk_nofoucused)))
                .setFirstSelectedPosition(0)//设置默认选择的按钮
                .initialise();
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        CommunityFragment mCommunityFragment = new CommunityFragment();
                        getSupportFragmentManager().beginTransaction().replace(R.id.tb, mCommunityFragment).commit();
                        break;
                    case 1:
                        ClassroomFragment mClassroomFragment = new ClassroomFragment();
                        getSupportFragmentManager().beginTransaction().replace(R.id.tb, mClassroomFragment).commit();

                        break;
                    case 2:
                        QuestionFragment mQuestionFragment = new QuestionFragment();
                        getSupportFragmentManager().beginTransaction().replace(R.id.tb, mQuestionFragment).commit();

                        break;
                    case 3:
                        DynamicFragment mDynamicFragment = new DynamicFragment();
                        getSupportFragmentManager().beginTransaction().replace(R.id.tb, mDynamicFragment).commit();
                        break;
                    default:

                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }


    class MyBaseMenuAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return menuList.size();
        }

        @Override
        public Object getItem(int i) {
            return menuList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(MainActivity.this, R.layout.menu_list_item, null);
            TextView mTvUserName = (TextView) view.findViewById(R.id.id_tv_menu_name);
            mTvUserName.setText((String) menuList.get(i).get("name"));
            ImageView mIvImageView = (ImageView) view.findViewById(R.id.id_iv_menu_icon);
            mIvImageView.setImageResource(menuIconList.get(i));
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(700, 150);
            view.setLayoutParams(param);
            return view;
        }
    }


}

